<?php
/**
 * Created by IntelliJ IDEA.
 * User: dhy
 * Date: 2015/9/11
 * Time: 22:27
 */

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller{


    public function index(){
        $articles = Article::all();
        $title = '<span style="color: red">文章</span>标题1';
        $intro = '文章一的简介';
        $first = "jelly";
        $last = "bool";
        $names = ['jelly','bool'];
        return view('articles.lists',compact('title','intro','first','last','names','articles'));
    }

    public function getArticles($id){
        $article = Article::find($id);
        return response()->json($article);
    }

    public function save(Request $request){
       // Article::create($request->all());
        $article = new Article();
        $article->title = $request->input("title");
        $article->content = $request->input("content");
        $article->save();

        return response()->json($article);
    }

    public function delete($id){
        Article::destroy($id);
        return "success";
    }

    public function update(Request $request){
        $id = $request->input('id');
        $title = $request->input("title");
        $content = $request->input('content');
        print_r([$id,$title,$content]);
        Article::where('id',$id)->update(['title'=>$title,'content'=>$content]);
        return 'success';
//        $article = Article::find($id);
//        $article->title = $request->input('title');
//        $article->content =  $request->input('content');
//
//        $article->save();
//
//        return response()->json($article);
    }
}